<?php
	
	App::import('Model', 'Model');
	
	class TestBase extends AppModel {
		var $name = 'TestBase';
		var $cacheSources = false;
		var $useDbConfig = 'test';
		var $hasMany = array('TestChild' => array('foreignKey'=>'base_id'));
		var $actsAs = array('LilSearch');
		
		function __construct($id = false, $table = null, $ds = null) {
			if (isset($GLOBALS['LilSearchTest'])) $this->actsAs = array('LilSearch' => $GLOBALS['LilSearchTest']);
			parent::__construct($id, $table, $ds);
		} 
	}
	
	class TestChild extends AppModel {
		var $name = 'TestChild';
		var $cacheSources = false;
		var $useDbConfig = 'test';
		var $belongsTo = array('TestBase' => array('foreignKey'=>'base_id'));
	}
	
	
	class LilSearchCase extends CakeTestCase {
		var $Base = null;
		var $fixtures = array('test_base', 'test_child');
	
		function start() {
			parent::start();
			App::import('Core', 'Folder');
			$f = new Folder();
			$f->delete(TMP.'lucene'.DS.'TestBase');
			
			$GLOBALS['LilSearchTest']['indexPath'] = '{TMP}lucene{DS}LilSearchTest';
			$this->Base =& new TestBase();
		}
		
		function end() {
			if (!empty($this->Base->Behaviors->LilSearch->settings['TestBase']['indexPath'])) {
				$indexPath = $this->Base->Behaviors->LilSearch->settings['TestBase']['indexPath'];
			} else {
				$indexPath = TMP.'lucene'.DS.'TestBase';
			}
			
			App::import('Core', 'Folder');
			$f = new Folder();
			$f->delete($indexPath);
			
			parent::end();
			
		}
	
		/* FIRST PART - WITHOUT FIELDS */
		
		function testTestBaseInstance() {
			$this->assertTrue(is_a($this->Base, 'TestBase'));
		}
		
		function testTestBaseFind() {
			$result = $this->Base->recursive = -1;
			$result = $this->Base->find('first');
			$this->assertTrue(!empty($result));
			$expected = array(
				'TestBase' => 
					array (
					'id' => '1',
					'name' => 'This is first title',
					'description' => 'This is a text description. I will be talking about Lil CakePHP plugins.',
					'created' => '2008-01-23 12:34:56',
					'modified' => '2008-03-21 12:34:56',
					),
				);
			$this->assertEqual($result, $expected);
		}
		
		function testTestBaseIndex() {
			$result = $this->Base->recursive = 1;
			$result = $this->Base->lilSearchIndex(1);
			$result = $this->Base->lilSearchIndex(2);
		}
		
		function testTestBaseLilSearchCount() {
			$result = $this->Base->lilSearchCount();
			$this->assertEqual($result, 2);
		}
		
		function testTestBaseLilSearchGetDocId() {
			$result = $this->Base->lilSearchGetDocId(2);
			$this->assertEqual($result, 1);
			
			$result = $this->Base->lilSearchGetDocId(1);
			$this->assertEqual($result, 0);
		}
		
		function testTestBaseLilSearch() {
			$expected = array('1');
			$result = $this->Base->lilSearch('CakePHP');
			$this->assertEqual($result, $expected);
			
			$expected = array('2');
			$result = $this->Base->lilSearch('"second title"');
			$this->assertEqual($result, $expected);
			
			$expected = array('1', '2');
			$result = $this->Base->lilSearch('this');
			$this->assertEqual($result, $expected);
			
			$expected = array();
			$result = $this->Base->lilSearch('notexistant');
			$this->assertEqual($result, $expected);
			
			$expected = array('1');
			$result = $this->Base->lilSearch('2008/08/20');
			$this->assertEqual($result, $expected);
			
			$expected = array('1');
			$result = $this->Base->lilSearch('prašiček');
			$this->assertEqual($result, $expected);
			
			$expected = array('1');
			$result = $this->Base->lilSearch('PRAŠIČEK');
			$this->assertEqual($result, $expected);
		}
		
		function testTestBaseLilSearchDelete() {
			$result = $this->Base->lilSearchDelete(2);
			$result = $this->Base->lilSearchCount();
			$this->assertEqual($result, 1);
		}
		
		/* SECOND PART - store fields */
		
		function testTestBaseInit2() {
			$this->end();
			$GLOBALS['LilSearchTest']['storeFields'] = true;
			$this->start();
		}
		
		function testTestBaseInstance2() {
			$this->assertTrue(is_a($this->Base, 'TestBase'));
		}
		
		function testTestBaseFind2() {
			$result = $this->Base->recursive = -1;
			$result = $this->Base->find('first');
			$this->assertTrue(!empty($result));
			$expected = array(
				'TestBase' => 
					array (
					'id' => '1',
					'name' => 'This is first title',
					'description' => 'This is a text description. I will be talking about Lil CakePHP plugins.',
					'created' => '2008-01-23 12:34:56',
					'modified' => '2008-03-21 12:34:56',
					),
				);
			$this->assertEqual($result, $expected);
		}
		
		function testTestBaseIndex2() {
			$result = $this->Base->recursive = 1;
			$result = $this->Base->lilSearchIndex(1);
			$result = $this->Base->lilSearchIndex(2);
		}
		
		function testTestBaseLilSearchCount2() {
			$result = $this->Base->lilSearchCount();
			$this->assertEqual($result, 2);
		}
		
		function testTestBaseLilSearchGetDocId2() {
			$result = $this->Base->lilSearchGetDocId(2);
			$this->assertEqual($result, 1);
			
			$result = $this->Base->lilSearchGetDocId(1);
			$this->assertEqual($result, 0);
		}
		
		function testTestBaseLilSearch2() {
			$expected1 = array (
				'TestBase' => 
					array (
					'id' => '1',
					'name' => 'This is first title',
					'description' => 'This is a text description. I will be talking about Lil CakePHP plugins.',
					'created' => '2008-01-23 12:34:56',
					'modified' => '2008-03-21 12:34:56',
					),
				'TestChild' => 
					array (
						0 => 
							array (
								'id' => '1',
								'base_id' => '1',
								'title' => 'LilSearch Behavior',
								'description' => 'This behavior implements Zend Lucene Search. It supports Utf-8 words like prašiček and searching numbers like 2008/08/20',
								'created' => '2008-01-23 12:34:56',
								'modified' => '2008-03-21 12:34:56',
							),
						1 => 
							array (
								'id' => '2',
								'base_id' => '1',
								'title' => 'LilBayes Behavior, Component and Vendor',
								'description' => 'This is a Bayes Categorization component.',
								'created' => '2008-04-05 11:22:33',
								'modified' => '2008-11-22 11:22:33',
							),
					),
			);
			$expected2 = array (
				'TestBase' => 
					array (
					'id' => '2',
					'name' => 'This is second title',
					'description' => '',
					'created' => '2008-04-05 11:22:33',
					'modified' => null,
					),
			);	
			$result = $this->Base->lilSearch('CakePHP');
			$this->assertEqual($result, array(0=>$expected1));
			
			$expected = array('2');
			$result = $this->Base->lilSearch('"second title"');
			$this->assertEqual($result, array(0=>$expected2));
			
			$result = $this->Base->lilSearch('this');
			$this->assertEqual($result, array(0=>$expected1, 1=>$expected2));
			
			$expected = array();
			$result = $this->Base->lilSearch('notexistant');
			$this->assertEqual($result, $expected);
			
			$expected = array('1');
			$result = $this->Base->lilSearch('2008/08/20');
			$this->assertEqual($result, array(0=>$expected1));
			
			$expected = array('1');
			$result = $this->Base->lilSearch('prašiček');
			$this->assertEqual($result, array(0=>$expected1));
			
			$expected = array('1');
			$result = $this->Base->lilSearch('PRAŠIČEK');
			$this->assertEqual($result, array(0=>$expected1));
			
			$result = $this->Base->lilSearch(array('conditions'=>'this', 'order'=>'TestBase.created'));
			$this->assertEqual($result, array(0=>$expected1, 1=>$expected2));
			
			$result = $this->Base->lilSearch(array('conditions'=>'this', 'order'=>'TestBase.created DESC'));
			$this->assertEqual($result, array(0=>$expected2, 1=>$expected1));
			
			$result = $this->Base->lilSearch(array('conditions'=>'this', 'order'=>array('TestBase.created DESC', 'TestBase.description')));
			$this->assertEqual($result, array(0=>$expected2, 1=>$expected1));
		}
		
		function testTestBaseLilSearchDelete2() {
			$result = $this->Base->lilSearchDelete(2);
			$result = $this->Base->lilSearchCount();
			$this->assertEqual($result, 1);
		}
	}
?>